/**
 * Copyright (c) 2011 Roundarch (www.roundarch.com)
 *
 * Author Joshua Jung
 *
 * ---
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.roundarch.flex.controls.intervals
{
    import com.roundarch.flex.controls.graphControlInterfaces.IInterval;
    import flash.events.Event;

    /**
     *
     * @author jjung
     */
    public class NumberIntervalCollection extends IntervalCollectionBase
    {
        //---------------------------------------------------------------------
        //
        //  Properties
        //
        //---------------------------------------------------------------------
        //-----------------------------
        // range
        //-----------------------------
        /**
         * @private
         */
        protected var _range : IInterval = new Interval(0, 100);

        [Bindable("rangeChanged")]
        /**
         *
         *
         * @default new Interval(0,100)
         */
        public function get range() : IInterval
        {
            return _range;
        }

        /**
         * @private
         */
        public function set range(value : IInterval) : void
        {
            if (value == _range)
            {
                return;
            }

            _range = value;

            dispatchEvent(new Event("rangeChanged"));
        }

        //-----------------------------
        // roundIntervalStart
        //-----------------------------
        /**
         * @private
         */
        protected var _roundIntervalStart : Boolean = true;

        [Bindable("roundIntervalStartChanged")]
        /**
         * When true, every interval start is rounded to the nearest integer, thus eliminating
         * floating-point errors when retrieving large collections of intervals.
         *
         * @default true
         */
        public function get roundIntervalStart() : Boolean
        {
            return _roundIntervalStart;
        }

        /**
         * @private
         */
        public function set roundIntervalStart(value : Boolean) : void
        {
            if (value == _roundIntervalStart)
            {
                return;
            }

            _roundIntervalStart = value;

            dispatchEvent(new Event("roundIntervalStartChanged"));
        }

        //-----------------------------
        // step
        //-----------------------------
        /**
         * @private
         */
        protected var _step : Number = 0.0000000001;

        [Bindable("stepChanged")]
        /**
         *
         *
         * @default 0.0000000001
         */
        public function get step() : Number
        {
            return _step;
        }

        /**
         * @private
         */
        public function set step(value : Number) : void
        {
            if (value == _step)
            {
                return;
            }

            _step = value;

            dispatchEvent(new Event("changed"));
            dispatchEvent(new Event("stepChanged"));
        }

        //-----------------------------
        // minDiscreteStep
        //-----------------------------
        /**
         * @private
         */
        protected var _minDiscreteStep : Number = 0.000000001;

        [Bindable("minDiscreteStepChanged")]
        /**
         *
         *
         * @default 0.000000001
         */
        public function get minDiscreteStep() : Number
        {
            return _minDiscreteStep;
        }

        /**
         * @private
         */
        public function set minDiscreteStep(value : Number) : void
        {
            if (value == _minDiscreteStep)
            {
                return;
            }

            _minDiscreteStep = value;

            dispatchEvent(new Event("minDiscreteStepChanged"));
        }

        //-----------------------------
        // ready
        //-----------------------------
        override public function get ready() : Boolean
        {
            return range != null;
        }

        //---------------------------------------------------------------------
        //
        //  Constructor
        //
        //---------------------------------------------------------------------
        /**
         *  Constructor
         */
        public function NumberIntervalCollection()
        {
        }

        //---------------------------------------------------------------------
        //
        //  Methods
        //
        //---------------------------------------------------------------------
        override public function getIntervalFor(value : Number) : IInterval
        {
            var intervalStart : Number = range.start + Math.round((value - range.start) / step);

            return new Interval(intervalStart, intervalStart + (step - minDiscreteStep));
        }

        override public function getIntervalsFor(startValue : Number, endValue : Number) : Array
        {
            if (endValue < startValue)
            {
                throw new Error("An error occurred retrieiving the intervals. The endValue was less than the startValue.");
            }

            var val : Number = startValue;
            var ret : Array = [];

            while (val <= endValue)
            {
                var interval : IInterval = getIntervalFor(val);
                ret.push(interval);
                val = interval.end + minDiscreteStep;

                if (roundIntervalStart)
                {
                    val = Math.round(val);
                }
            }

            return ret;
        }
    }
}
